# -------------------------------------------------------------------
#       -*- coding: utf-8 -*-
#   @Project    :   spider_biqushen
#   @File       :   kaiyankanshu.py
#   @Author     :   WANGYU
#   @Time       :   2021-09-02 08:16:26
#   @Software   :   PyCharm
#   @Desc       :   快眼看书爬虫
# -------------------------------------------------------------------


import requests
import time
from bs4 import BeautifulSoup


def get_html(novel_url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36'
                      ' (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'
    }
    proxies = {
        'http': 'http://125.77.130.17:80',
        'https': 'https://125.77.130.17:80'
    }
    response = requests.get(novel_url, headers=headers)
    response.encoding = 'utf-8'
    return response


def get_novel():
    for i in range(1, 2):
        url = f'https://www.ibotaodz.com/novel/337679/read_{i}.html'
        resp = get_html(url)
        # print(resp.text)
        page = BeautifulSoup(resp.text, 'html.parser')
        booktext = page.find('div', attrs={'id': 'chaptercontent'})
        print(booktext.text)

        title = page.find('div', attrs={'class': 'info'}).find('span').find('a').get('title')
        with open(title + '.txt', 'a') as novel:
            novel.write(booktext.text)

        print(f'-----------------------第{i}章下载完成！---------------------')
        time.sleep(2)


if __name__ == '__main__':
    get_novel()
